package com.yingma.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yingma.common.util.AjaxResult;
import com.yingma.system.dao.JcSysMenuDao;
import com.yingma.system.entity.JcSysMenu;
import com.yingma.system.entity.JcVehicle;
import com.yingma.system.service.JcSysMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (JcSysMenu)表控制层
 *
 * @author makejava
 * @since 2020-03-08 15:28:51
 */
@RestController
@RequestMapping("jcSysMenu")
public class JcSysMenuController {
    /**
     * 服务对象
     */
    @Resource
    private JcSysMenuService jcSysMenuService;


    @Resource
    private JcSysMenuDao jcSysMenuDao;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public JcSysMenu selectOne(String id) {
        return this.jcSysMenuService.queryById(id);
    }


    /**
     * 查询一级菜单列表
     *
     * @return
     */
    @ApiOperation("查询一级菜单列表")
    @RequestMapping(value = "selectOneMenu", method = RequestMethod.GET)
    public AjaxResult selectSysMenu() {
        List<JcSysMenu> SysMenulist=null;
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        //redis中有SysMenulist就从缓存中取数据
        if(redisTemplate.hasKey("SysMenulist")){
            SysMenulist=(List<JcSysMenu>)operations.get("SysMenulist");
        }else {
            //如果redis缓存中没有就去数据库中查询
            //得到一级菜单列表  SysMenulist
            SysMenulist = jcSysMenuService.selectSysMenu();
            JcSysMenu jcSysMenu = new JcSysMenu();
            //声明数组存放二级菜单列表
            List<JcSysMenu> secondlist = new ArrayList<JcSysMenu>();
            for (int i = 0; i < SysMenulist.size(); i++) {
                //给二级菜单赋值
                SysMenulist.get(i).setSecondlist(jcSysMenuService.selectnextMenu(SysMenulist.get(i).getId()));
                for (int j = 0; j < SysMenulist.get(i).getSecondlist().size(); j++) {
                    //得到二级菜单的ID值
                    String secondId = SysMenulist.get(i).getSecondlist().get(j).getId();
                    JcSysMenu jcSysMenu1 = new JcSysMenu();
                    jcSysMenu1.setZid(secondId);
                    QueryWrapper<JcSysMenu> queryWrapper = new QueryWrapper<JcSysMenu>(jcSysMenu1);
                    List<JcSysMenu> thirdlist = jcSysMenuDao.selectList(queryWrapper);
                    if (thirdlist.size() > 0) {
                        SysMenulist.get(i).getSecondlist().get(j).setThirdlist(thirdlist);
                    }
                }
            }
            //查询完数据库中的数据之后将查询到的数据放入redis缓存中方便下次查询
            operations.set("SysMenulist",SysMenulist);
        }
        return AjaxResult.success(SysMenulist);
    }



}
